package com.tm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tm.mapper.UserMapper;
import com.tm.service.userService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tm.pojo.TodayBeauty;
import tm.pojo.UserPojo;
import tm.pojo.dto.HomePageDto;

import java.util.Date;
import java.util.List;

/**
 * @author Rocky
 * @create 2022-09-25 19:10
 */
@Service
public class userServiceImpl implements userService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public void addUserNumber(String s) {
        userMapper.addUserNumber(s);

    }

    @Override
    public void addUser(UserPojo userPojo) {
        userMapper.updateUser(userPojo);
        //创建一个批量操作对象，用来批量新增
        BulkRequest bulkRequest = new BulkRequest();
        //创建索引时，一个参数就是直接创建索引，使用默认的_doc文档类型

            //两个参数，第二个参数就是自定义的类型，但是已经不建议使用
            IndexRequest indexRequest = new IndexRequest("th-zkj", "user");
            indexRequest.source(JSONObject.toJSONString(userPojo), XContentType.JSON);
            //将索引加入到批量操作对象
            bulkRequest.add(indexRequest);
        try {
            //通过高层对象将数据操作进入ES
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public UserPojo queryNumber(String phonenumber) {
        return userMapper.queryNumber(phonenumber);
    }


}
